function truncate(mydata, mylength) {
	
	if (null == mydata || 0 == mydata.length){ 
		return "";
	} else if(mylength >= mydata.length || mylength < 3) {
		return mydata;
	} else {
		return mydata.substring(0, mylength-3)+" ...";
	}
}

function truncateNumber(mydata, mylength) {
	
	var myMax="1";
	var myMin="1";
	var n=0;
	for(n=0; n < mylength-1; n++) {
		myMin = myMin + "0";
	}
	
	myMin = parseInt(myMin, 10);
	myMax = myMin*10-1;

	if (0 == mydata){ 
		return 0;
	} else if(myMin >= mydata || mylength < 3) {
		return mydata.toString();
	} else if(mydata >= myMin && mydata <= myMax){
		return mydata.toString();
	} else {
		for(n=0; n < mylength-1; n++) {
			mydata = mydata/10;
			if(mydata>=myMin && mydata <= myMax ) {
				mydata = parseInt(mydata/1000);
				break;
			}
		}
		return mydata.toString()+" ...";
	}
}

function truncateDateTime(dateTime) {
	if (true == isNull(dateTime)){ 
		return "";
	} else {
		return dateTime.substring(0, dateTime.length - 2);
	}
}

function truncateDate(dateTime) {
	if (true == isNull(dateTime)){ 
		return "";
	} else {
		return dateTime.substring(0, 10);
	}
}

function isNullDate(str){
	if( null == str || "" == str ){
		return (new Date()).format("yyyy-MM-dd");
	}else{
		return str;
	}
}

function isNull(str){
	if( null == str || "" == str ){
		return true;
	}else{
		return false;
	}
}

String.prototype.Trim = function() { 
return this.replace(/(^\s*)|(\s*$)/g, ""); 
};

function confirmData(data) {
	var str = "";
	for (var i in data) {
		if (typeof data[i] == 'object') {
			for (var j in data[i]) {
				str += i+"."+j+"="+encodeURIComponent(data[i][j])+"&";
			}
		} else {
			str += i+"="+encodeURIComponent(data[i])+"&";
		}
	}
	return str.substring(0,str.length-1);
}

function isIntNumber(numberString) {
	var number = Number(numberString);
	if(NaN == number && -1 != numberString.indexOf(".")){
		return false;
	}else{
		return true;
	}
}

function isValidPath(path) {
	var pattern = /^[C|D|E|F|c|d|e|f]:\\./;
	if(!pattern.exec(path)) {
		return false;
	}else{
		return true;
	}
}

function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

// Example:192.168.1.1:80
function isValidIPAddress(ipAddressString){
	re=/^(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])(\:\d+)*$/;
	return re.test(ipAddressString);
}

// "%20ABC%20" -> " ABC "
function replaceSpace(mydata) {
	if(-1 != mydata.indexOf("%20")) {
		return mydata.replace(/%20/g, " ");
	}
}

// Example: 0.0026 -> 0.003, if length = 3
// wait to be deleted
function fixedNum(value, length) {
	var strValue = value.toString();
	var intValue = parseInt(value);
	var decimal = value - intValue;
	var decimalFixed = value.toFixed(length);
	var returnValue;
	
	//when value="5e-8": 1/20000000 = 0.00000005
//	alert("value = " + value);
	
	if(value >= 1) {
		return value;
	} else if(0 == decimal) {
		return value;
	} else if(value == decimalFixed) {
		return value;
	} else if(-1 != strValue.toString().indexOf("e")) {
		tempInputMapResX2 = inputMapResX.toFixed(8);
	} else {
//		return decimalFixed;
		var subValue = (value + 0.00000001).toString().substring(0, 10);
		return parseFloat(subValue);
	}
}

// LSB計算
function calLSB(LsbC, LsbM) {
	
	if(true == isNaN(LsbC) || 0 == parseInt(LsbC) || true == isNull(LsbC)) {
		return "lsbCError";
	} else if(true == isNaN(LsbM) || 0 == parseInt(LsbM) || true == isNull(LsbM)) {
		return "lsbMError";
	}
	
	var res = LsbC / LsbM;
	var strRes = res.toString();
	var strResPlus = (res + 0.00000001).toString();
	var lengthRes = strRes.length;
	var ePosition = 0;
	var returnValue = res;
	var exp = 0;
	var calZero="";
	
	if(res < 1) {
		ePosition = strResPlus.indexOf("e");
		
		if(-1 == ePosition) {
			if(lengthRes > 10) {
				returnValue = strResPlus.substr(0, 10);
			}
		} else {
			exp = parseInt(strResPlus.substr(ePosition+2, 2));
			
			for(var i = 0; i < exp-1; i ++) {
				calZero += "0";
			}
			returnValue = "0." + calZero + strResPlus.substr(0, 1) + strResPlus.substr(2, 8-exp);
		}
	}
	return returnValue;
}

// 内部値->物理値
function calMinMaxPhy(lsbC, lsbM, intValue) {
	if(true == isNaN(lsbC) || 0 == parseInt(lsbC) || true == isNull(lsbC)) {
		return "lsbCError";
	} else if(true == isNaN(lsbM) || 0 == parseInt(lsbM) || true == isNull(lsbM)) {
		return "lsbMError";
	} else if(true == isNaN(intValue) || true == isNull(intValue) || (true == isNull(parseInt(intValue)) && 0 != parseInt(intValue))) {
		return "intValueError";
	} else {
		return parseInt(intValue) * parseInt(lsbC) / parseInt(lsbM);
	}
}

// 物理値->内部値
function calMinMaxInt(lsbC, lsbM, phyValue) {
	if(true == isNaN(lsbC) || 0 == parseInt(lsbC) || true == isNull(lsbC)) {
		return "lsbCError";
	} else if(true == isNaN(lsbM) || 0 == parseInt(lsbM) || true == isNull(lsbM)) {
		return "lsbMError";
	} else if(true == isNaN(phyValue) || true == isNull(phyValue) || (true == isNull(parseFloat(phyValue)) && 0 != parseFloat(phyValue))) {
		return "phyValueError";
	} else {
		return parseInt(parseFloat(phyValue) * parseInt(lsbM) / parseInt(lsbC));
	}
}

// Z軸要素数計算
function calZElements(dimension, elemXValue, elemYValue) {
	var intDimension = parseInt(dimension);
	var intElemXValue;
	var intElemYValue;
	
	if(true == isNaN(elemXValue) || true == isNull(elemXValue) || true == isNull(parseInt(elemXValue))) {
		return "elemXValueError";
	} else if((1==dimension) && (true == isNaN(elemYValue) || true == isNull(elemYValue) || true == isNull(parseInt(elemYValue))) ) {
		return "elemYValueError";
	}
	
	intElemXValue = parseInt(elemXValue);
	intElemYValue = parseInt(elemYValue);
	
	if(1 == dimension) {  // 3D
		return intElemXValue * intElemYValue;
		
	} else {  // 2D
		return parseInt(elemXValue);
	}
}

function dateFormatStringCompare(str1,str2) {
	date1 = str1.split("-");
	date2 = str2.split("-");
	for(var i=0;i<3; i++) {
		if(date1[i] > date2[i]) {
			return 1;
		} else if(date1[i] < date2[i]) {
			return -1;
		}
	}
	return 0;
}

function calResoLsb(lsbC, lsbM, resolution) {
	var intLsbC = parseInt(lsbC);
	var intLsbM = parseInt(lsbM);
	var floatResolution = parseFloat(resolution);
	
	if(floatResolution >= (lsbC/lsbM)) {
		return true;
	} else {
		return false;
	}
}

function getMinByAtt(mapAttValue) {
	if("UB" == mapAttValue) {
		return 0;
	} else if("SB" == mapAttValue) {
		return -128;
	} else if("UW" == mapAttValue) {
		return 0;
	} else if("SW" == mapAttValue) {
		return -32768;
	} else if("UD" == mapAttValue) {
		return 0;
	} else if("SD" == mapAttValue) {
		return -2147483648;
	}
}

function getMaxByAtt(mapAttValue) {
	if("UB" == mapAttValue) {
		return 255;
	} else if("SB" == mapAttValue) {
		return 127;
	} else if("UW" == mapAttValue) {
		return 65535;
	} else if("SW" == mapAttValue) {
		return 32767;
	} else if("UD" == mapAttValue) {
		return 4294967295;
	} else if("SD" == mapAttValue) {
		return 2147483647;
	}
}

function escapeReplace(aString) {
	return aString.toString().replace(/\"/g,"\\\"");
}

function matchVersion(versionId, versionName, versionList) {
	var versionListLen = versionList.length;
	var i = 0;
	
	if(true == isNull(versionId) || true == isNull(versionName)) {
		return false;
	}
	
	for(i = 0; i < versionListLen; i ++) {
		if(versionId == versionList[i]['versionId']) {
			return true;
		}
	}
	return false;
}

function matchUnitList(value, list) {
	var listLen = list.length;
	var i = 0;
	
	if(true == isNull(value) || null == list) {
		return false;
	}
	
	for(i = 0; i < listLen; i ++) {
		if(value == list[i]['unit']) {
			return true;
		}
	}
	return false;
}

function analysisVersionList(versionList) {
	var i = 0;
	var strVersionList = "";
	
	for(i = 0; i < versionList.length; i ++) {
		strVersionList += "<option value='" + versionList[i]['versionName'] + "'>" + versionList[i]['versionName'] +"</option>"; 
	}
	return strVersionList;
}

function checkSearchDate(startDate, endDate) {
	if(false == isNull(startDate) && false == isNull(endDate) && (startDate > endDate)) {
		alert("正しい日付を入力してください");
		return false;
	}
	return true;
}

function changeNullValue(myValue) {
	if(null == myValue || "" == myValue) {
		return "";
	} else {
		return myValue;
	}
}

function loadjscssfile(filename,filetype) {

	if(filetype == "js") {
		var fileref = document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src",filename);
		
	} else if(filetype == "css") {
		var fileref = document.createElement('link');
		fileref.setAttribute("rel","stylesheet");
		fileref.setAttribute("type","text/css");
		fileref.setAttribute("href",filename);
	}
	if(typeof fileref != "undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);
	}
}